import java.util.*;
import java.util.Scanner;
public class BinaryTree {
    public BinaryTree() {
    }

    static class TreeNode {
        public int data;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int data) {
            this.data = data;
        }
    }

    public TreeNode root;

    public void createTree() {
        TreeNode A = new TreeNode(1);
        TreeNode B = new TreeNode(2);
        TreeNode C = new TreeNode(2);
        TreeNode D = new TreeNode(4);
        TreeNode E = new TreeNode(5);
        TreeNode F = new TreeNode(5);
        TreeNode G = new TreeNode(4);
//        TreeNode H = new TreeNode(8);

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
//        E.right = H;
        this.root = A;
    }

    public void preOrder(TreeNode root) {
        if (root == null) return;
        System.out.print(root.data + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public void inOrder(TreeNode root) {
        if (root == null) return;
        inOrder(root.left);
        System.out.print(root.data + " ");
        inOrder(root.right);
    }

    public void postOrder(TreeNode root) {
        if (root == null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.data + " ");

    }

    /*   List<Integer> list = new ArrayList<>();

       public List<Integer> preorderTraversal(TreeNode root) {
           if (root == null) {
               return list;
           }
           list.add(root.data);
           preorderTraversal(root.left);
           preorderTraversal(root.right);
           return list;
       }*/
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();

        if (root == null) {
            return ret;
        }
        List<Integer> leftroot = inorderTraversal(root.left);
        ret.addAll(leftroot);
        ret.add(root.data);
        List<Integer> rightroot = inorderTraversal(root.right);
        ret.addAll(rightroot);
        return ret;

    }

    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();

        if (root == null) {
            return ret;
        }
        List<Integer> leftroot = postorderTraversal(root.left);
        ret.addAll(leftroot);
        List<Integer> rightroot = postorderTraversal(root.right);
        ret.addAll(rightroot);
        ret.add(root.data);
        return ret;
    }

    int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }

    public static int nodeSize;

    public void size2(TreeNode root) {
        if (root == null) {
            return;
        }
        nodeSize++;
        size2(root.left);
        size2(root.right);
    }

    int getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);


    }

    public static int leafsize;

    void getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            leafsize++;
        }
        getLeafNodeCount2(root.left);
        getLeafNodeCount2(root.right);
    }

    int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) + getKLevelNodeCount(root.right, k - 1);

    }

    /* int getHeight(TreeNode root){
         if (root == null){
             return 0;
         }
         int le=getHeight(root.left);
         int ri=getHeight(root.right);
         return (le>ri?le+1:ri+1);//Math.max(getHeight(root.left),getHeight(root.right))+1

     }
     public boolean isBalanced(TreeNode root) {
         if(root==null){
             return true;
         }
         int le=getHeight(root.left);
         int ri=getHeight(root.right);
        return Math.abs(le-ri)<=1&&isBalanced(root.left)&&isBalanced(root.right);


     }*/
    TreeNode find(TreeNode root, int data) {
        if (root == null) {
            return null;
        }
        if (root.data == data) {
            return root;
        }
        TreeNode ret1 = find(root.left, data);
        if (ret1 != null) {
            return ret1;
        }
        TreeNode ret2 = find(root.right, data);
        if (ret2 != null) {
            return ret2;
        }
        return null;
    }

    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p != null && q == null || p == null && q != null) {
            return false;
        }
        if (p == null && q == null) {
            return true;
        }
        if (p.data != q.data) {
            return false;
        }
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);


    }

    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root == null) {
            return false;
        }
        if (isSameTree(root, subRoot)) {
            return true;
        }
        if (isSubtree(root.left, subRoot)) {
            return true;
        }
        if (isSubtree(root.right, subRoot)) {
            return true;
        }
        return false;
    }

    int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int le = getHeight(root.left);
        int ri = getHeight(root.right);

        if (le >= 0 && ri >= 0 && Math.abs(le - ri) <= 1) {
            return Math.max(le, ri) + 1;
        } else {
            return -1;
        }

    }

    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        return getHeight(root) >= 0;


    }

    /* public boolean isSymmetric(TreeNode root) {
             if (root ==null){
                 return true;
             }
             return isSymmetricChild(root.left,root.right);
     }
     public boolean isSymmetricChild(TreeNode LeftTree,TreeNode RightTree){
             if (LeftTree==null&&RightTree!=null||LeftTree!=null&&RightTree==null){
                 return false;
             }
         if (LeftTree==null&&RightTree==null){
             return true;
         }
             if (LeftTree.data!=RightTree.data){
                 return false;
             }
             return isSymmetricChild(LeftTree.left,RightTree.right)&&isSymmetricChild(LeftTree.right,RightTree.left);

     }*/
    void LevelOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.println(cur.data);
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }

    }

   /* boolean isCompleteTree(TreeNode root) {

    }*/

    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> row = new ArrayList<>();
            while (size > 0) {

                TreeNode cur = queue.poll();
                size--;
                System.out.println(cur.data);
                row.add(cur.data);
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }

            }
            ret.add(row);
        }
        return ret;
    }

    public boolean isSymmetric(TreeNode root) {
        if(root == null) {
            return true;
        }
        return isSymmetricChild(root.left,root.right);

    }

    public boolean isSymmetricChild(TreeNode LeftTree, TreeNode RightTree) {
        if (LeftTree==null&&RightTree!=null||LeftTree!=null&&RightTree==null) {
            return false;
        }
        if (LeftTree==null&&RightTree==null) {
            return true;
        }
        if (LeftTree.data!=RightTree.data){
            return false;
        }
        return isSymmetricChild(LeftTree.left,RightTree.right)&&isSymmetricChild(LeftTree.right,RightTree.left);

    }
    void levelOrder2(TreeNode root){
        if (root ==null){
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){

                TreeNode cur=queue.poll();
            System.out.println(cur.data+" ");
            if (cur.left!=null) {
                queue.offer(cur.left);
            }
            if (cur.right!=null) {
                queue.offer(cur.right);
            }

        }
    }
    public List<List<Integer>> levelOrder3(TreeNode root) {
        List<List<Integer>> row = new ArrayList<>();
        if (root ==null){
            return row;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {

                int curr=queue.size();
            List<Integer> ret=new ArrayList<>();
                while (curr>0) {
                TreeNode cur = queue.poll();
                curr--;
                ret.add(cur.data);
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
                row.add(ret);
        }
        return row;
    }
    boolean isCompleteTree(TreeNode root) {
        if (root == null) {
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            } else {
                break;
            }
            while (!queue.isEmpty()) {
                TreeNode ret = queue.peek();
                if (ret != null) {
                    return false;
                } else {
                    queue.poll();
                }
            }
        }
        return true;
    }
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null){
            return null;
        }
        if (p==root||q==root){
            return root;
        }
        TreeNode retLeft = lowestCommonAncestor(root.left,p,q);
        TreeNode retRight = lowestCommonAncestor(root.right,p,q);
        if (retLeft!=null&&retRight!=null){
            return root;
        }else if (retLeft!=null){
            return retLeft;
        }else {
            return retRight;
        }
    }
    public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null || p == null || q == null) {
            return null;
        }
        Stack<TreeNode> stack1 = new Stack<>();
        getPath(root, p, stack1);
        Stack<TreeNode> stack2 = new Stack<>();
        getPath(root, q, stack2);
        int size1 = stack1.size();
        int size2 = stack2.size();
        if (size1 > size2) {
            int tmp = size1 - size2;
            while (tmp != 0) {
                stack1.pop();
                tmp--;
            }
        } else {
            int tmp = size2 - size1;
            while (tmp != 0) {
                stack2.pop();
                tmp--;
            }
        }
        while (!stack1.empty()&&!stack2.empty()) {
            if (stack1.peek() == stack2.peek()) {
                return stack1.peek();
            } else {
                stack1.pop();
                stack2.pop();
            }
        }
        return null;
    }
    private  boolean getPath(TreeNode root,TreeNode node,Stack<TreeNode> stack){
        if (root==null||node==null){
            return false;
        }
        stack.push(root);
        if (root==node){
            return true;
        }
        boolean ret1 = getPath(root.left,node,stack);
        if (ret1){
            return true;
        }
        boolean ret2=getPath(root.right,node,stack);
        if (ret2){
            return true;
        }
        stack.pop();
        return false;

    }
    public TreeNode Convert(TreeNode pRootOfTree) {
        if (pRootOfTree ==null ){
            return null;
        }
        ConvertChild(pRootOfTree);
        TreeNode head = pRootOfTree;
        while (head.left!=null){
            head = head.left;
        }
        return head;
    }
    public TreeNode prev=null;
    public void ConvertChild(TreeNode root){
        if (root == null ){
            return ;
        }
        ConvertChild(root.left);
//        System.out.println(root.data+" ");
        root.left=prev;

        if (prev!=null) {
            prev .right=root;

        }
        prev = root;
        ConvertChild(root.right);
    }
/*public int preIndex=0 ;
    private TreeNode buildTreeChild(int[] preorder,int[] inorder , int inbegin , int inend){
        if (inbegin>inend){
            return null;
        }
        TreeNode root = new TreeNode(preorder[preIndex]);
        int rootIndex = findInorderIndex(inorder,preorder[preIndex],inbegin,inend);
        preIndex++;
        root.left =buildTreeChild(preorder,inorder,inbegin,rootIndex-1);
        root.right=buildTreeChild(preorder,inorder,rootIndex+1,inend);
        return root;
    }
    private  int findInorderIndex(int[] inorder,int val,int inbegin,int inend){
        for (int i = inbegin; i <=inend ; i++) {
            if (inorder[i] == val){
                return i;
            }
        }
        return -1;
    }
    public TreeNode buildTree(int[] preorder,int[] inorder){
        return buildTreeChild(preorder,inorder,0,inorder.length-1);
    }*/
    public TreeNode buildTree2(int[] inorder, int[] postorder) {
        postIndex=postorder.length-1;
        return buildTreeChild(inorder,postorder,0,inorder.length-1);
    }
    public int postIndex=0;
    private TreeNode buildTreeChild(int[] inorder,int[] postorder , int inbegin , int inend){
        if (inbegin>inend){
            return null;
        }
        TreeNode root = new TreeNode(postorder[postIndex]);
        int rootIndex = findInorderIndex(inorder,postorder[postIndex],inbegin,inend);
        postIndex--;
        root.left =buildTreeChild(inorder,postorder,inbegin,rootIndex-1);
        root.right=buildTreeChild(inorder,postorder,rootIndex+1,inend);
        return root;
    }
    private  int findInorderIndex(int[] inorder,int val,int inbegin,int inend){
        for (int i = inbegin; i <=inend ; i++) {
            if (inorder[i] == val){
                return i;
            }
        }
        return -1;
    }
    public String tree2str(TreeNode root) {
        StringBuilder sb =new StringBuilder();
        tree2strChild(root,sb);
        return sb.toString();

    }
    public void tree2strChild(TreeNode t,StringBuilder sb) {
        if (t == null){
            return;
        }
        sb.append(t.data);
        if (t.left!=null){
            sb.append("(");
            tree2strChild(t.left,sb);
            sb.append(")");
        }else {
            if (t.right==null){
                return;
            }else {
                sb.append("()");
            }

        }
        if (t.right==null){
            return;
        }else {
            sb.append("(");
            tree2strChild(t.right,sb);
            sb.append(")");
        }

   }
}

